#include <qqtvirtuallongdatamanager.h>

#include <qqthostaddressmanager.h>

QQtVirtualLongDataManager::QQtVirtualLongDataManager ( QObject* parent )
    : QObject ( parent )
{
    c0 = 0;
    p0 = 0;
}

QQtVirtualLongDataManager::~QQtVirtualLongDataManager() {}

void QQtVirtualLongDataManager::setClientHandler ( QQtUdpClient* handler )
{
    c0 = handler;
}

QQtUdpClient* QQtVirtualLongDataManager::handler()
{
    return c0;
}

void QQtVirtualLongDataManager::setProtocolHandler ( QQtLongDataProtocol* protocol )
{
    p0 = protocol;
    c0->installProtocol ( p0 );
    connect ( p0, SIGNAL ( readyRead ( const QQtLongDataSerialization& ) ),
              this, SIGNAL ( readyRead ( const QQtLongDataSerialization& ) ) );
}

QQtLongDataProtocol* QQtVirtualLongDataManager::protocol()
{
    return p0;
}

void QQtVirtualLongDataManager::sendMessage ( const QQtLongDataSerialization& data )
{
    p0->sendMessage ( data );
}

void QQtVirtualLongDataManager::setServerAddress ( QHostAddress address, int port )
{
    c0->setServerAddress ( address, port );
    p3line() << address.toString() << port;
}

void QQtVirtualLongDataManager::setServerPort ( int port )
{
    c0->setServerAddress ( QHostAddress ( QHostAddress::Broadcast ), port );
    p3line() << QHostAddress ( QHostAddress::Broadcast ) << port;
}

void QQtVirtualLongDataManager::setListeningAddress ( QHostAddress address, int port )
{
    c0->disconnectFromHost();
    c0->bind ( address, port, QQtUdpClient::DefaultForPlatform );
    p3line() << c0->localAddress() << c0->localPort();
}

void QQtVirtualLongDataManager::setListeningPort ( int port )
{
    c0->disconnectFromHost();
    QQtHostAddressManager manager;
    QString ip = manager.getHostAddress();
#if USE_HOST_ADDRESS
    c0->bind ( QHostAddress ( ip ), port, QQtUdpClient::DefaultForPlatform );
#else
    c0->bind ( port, QQtUdpClient::DefaultForPlatform );
#endif
    p3line() << c0->localAddress() << c0->localPort();
}
